﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using DAL;
using Entities;
using BUS;
using Utility;

public partial class A_LetsDeal_DoanhNghiep_chuyenkhoan : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["username_dn"] != null)
            {
                load_data();
            }
            else
            {
                Response.Redirect("~/A_LetsDeal-DoanhNghiep/Default.aspx");
            }
        }
    }

    protected void load_data()
    {
        string dk = "";
        string dk1 = "";
        int total = 0;
        string doanhnghiep = Session["username_dn"].ToString();
        Tbl_DoanhNghiep_Entities dn = Tbl_DoanhNghiep_BUS.GetByName(doanhnghiep);
        Tbl_SanPham_Collection sp = Tbl_SanPham_BUS.GetByIDDN(dn.STT);
        for (int i = 0; i < sp.Count; i++)
        {
            dk1 = dk1 + sp[i].STT.ToString() + ",";
            Tbl_CTDH_Collection ctdh = Tbl_CTDH_BUS.GetByIDSP(sp[i].STT);
            if (ctdh.Count > 0)
            {
                for (int j = 0; j < ctdh.Count; j++)
                {
                    dk = dk + ctdh[j].DonHang.ToString() + ",";
                }
            }
        }
        if (dk != "")
        {
            dk = dk.Substring(0, dk.Length - 1);
            Tbl_DonHang_Collection dhCollection =
                Tbl_DonHang_BUS.Get("where 1=1 and STT in (" + dk + ") and ThanhToanDN='False' and DaGiao='True'", pager.CurrentIndex, pager.PageSize,
                Tbl_DonHang_Columns.STT.ToString(), OrderDirection.ASC.ToString(), out total);
            Repeater1.DataSource = dhCollection;
            Repeater1.DataBind();
            pager.ItemCount = total;
        }
    }
    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem ||
                  e.Item.ItemType == ListItemType.Item)
        {
            Tbl_DonHang_Entities dh = (Tbl_DonHang_Entities)e.Item.DataItem;
            Tbl_CTDH_Entities ctdh = Tbl_CTDH_BUS.GetByIDDH(dh.STT);
            Tbl_SanPham_Entities sp = Tbl_SanPham_BUS.GetByID(ctdh.SanPham);

            HyperLink hyp_tensp = (HyperLink)e.Item.FindControl("hyp_tensp");
            Literal lit_loinhuan = (Literal)e.Item.FindControl("lit_loinhuan");
            Literal lit_madh = (Literal)e.Item.FindControl("lit_madh");
            Literal lit_slb = (Literal)e.Item.FindControl("lit_slb");
            CheckBox CheckBox1 = (CheckBox)e.Item.FindControl("CheckBox1");
            HiddenField HiddenField1 = (HiddenField)e.Item.FindControl("HiddenField1");

            lit_madh.Text = dh.MaDH.ToString();
            HiddenField1.Value = sp.STT.ToString();
            lit_slb.Text = ctdh.SoLuong.ToString();
            hyp_tensp.NavigateUrl = "#";
            if (sp.TenSP.Length >= 70)
            {
                hyp_tensp.Text = sp.TenSP.Substring(0, 70) + "...";
                hyp_tensp.ToolTip = sp.TenSP;
            }
            else
            {
                hyp_tensp.Text = sp.TenSP;
            }
            lit_loinhuan.Text = String.Format("{0:0,0 đ}", (ctdh.SoLuong * sp.GiaBan) - (ctdh.SoLuong * sp.GiaBan) / 10);
            CheckBox1.Checked = false;
        }
        if (e.Item.ItemType == ListItemType.Footer)
        {
            Literal lit_soduhienco = (Literal)e.Item.FindControl("lit_soduhienco");
            lit_soduhienco.Text = String.Format("{0:0,0 đ}", Tbl_DoanhNghiep_BUS.GetByName(Session["username_dn"].ToString()).SoDuTaiKhoan);
        }
    }
    protected void bt_update_Click(object sender, EventArgs e)
    {
        double loinhuan = 0;
        for (int i = 0; i < Repeater1.Items.Count; i++)
        {
            CheckBox CheckBox1 = (CheckBox)Repeater1.Items[i].FindControl("CheckBox1");
            if (CheckBox1.Checked == true)
            {
                HiddenField HiddenField1 = (HiddenField)Repeater1.Items[i].FindControl("HiddenField1");
                int masp = int.Parse(HiddenField1.Value);
                Literal lit_loinhuan = (Literal)Repeater1.Items[i].FindControl("lit_loinhuan");
                loinhuan = loinhuan + double.Parse(lit_loinhuan.Text.Substring(0,lit_loinhuan.Text.Length -1));
                Literal lit_madh = (Literal)Repeater1.Items[i].FindControl("lit_madh");
                Tbl_DonHang_BUS.Update_ThanhToanDN(lit_madh.Text);
            }
        }
        Tbl_DoanhNghiep_Entities dn = new Tbl_DoanhNghiep_Entities();
        dn.STT = Tbl_DoanhNghiep_BUS.GetByName(Session["username_dn"].ToString()).STT;
        dn.SoDuTaiKhoan = loinhuan;
        Tbl_DoanhNghiep_BUS.Update_SODU(dn);
        Response.Redirect(Request.RawUrl);
    }
}
